package com.wangyu.talents.web.system.resource.model;

import com.google.common.collect.Lists;
import com.wangyu.talents.common.utils.StringUtil;
import com.wangyu.talents.core.entity.DataEntity;
import java.util.List;

/**
 * 资源实体
 *
 * @author wangyu
 * @Date 2018-09-04 14:44
 */
public class Resource extends DataEntity<Resource> {

  /**
   * 名称
   */
  private String name;

  /**
   * 标识
   */
  private String flag;

  /**
   * target
   */
  private String target;

  /**
   * 地址
   */
  private String url;

  /**
   * 类别（1:菜单，2:按钮，3:portlet）
   */
  private Integer type = 1;

  /**
   * 权重
   */
  private Integer weight = 100;

  /**
   * 图标
   */
  private String icon;

  /**
   * 父资源
   */
  private Resource parent;


  /**
   * 子资源集合
   */

  private List<Resource> childrens;

  /**
   * 资源树
   */
  private String parentIds;

  /**
   * 描述
   */
  private String description;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getFlag() {
    return flag;
  }

  public void setFlag(String flag) {
    this.flag = flag;
  }

  public String getTarget() {
    return target;
  }

  public void setTarget(String target) {
    this.target = target;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public Integer getType() {
    return type;
  }

  public void setType(Integer type) {
    this.type = type;
  }

  public Integer getWeight() {
    return weight;
  }

  public void setWeight(Integer weight) {
    this.weight = weight;
  }

  public String getIcon() {
    return icon;
  }

  public void setIcon(String icon) {
    this.icon = icon;
  }

  public Long getParentId() {
    if (this.getParent() != null) {
      return this.getParent().getId();
    }
    return 0l;
  }

  public Resource getParent() {
    return parent;
  }

  public void setParent(Resource parent) {
    this.parent = parent;
  }

  public List<Resource> getChildrens() {
    return childrens;
  }

  public void setChildrens(List<Resource> childrens) {
    this.childrens = childrens;
  }

  public String getParentIds() {
    return parentIds;
  }

  public void setParentIds(String parentIds) {
    this.parentIds = parentIds;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }


  public void addChildren(Resource res) {
    List<Resource> childrens = this.getChildrens();
    if (childrens == null) {
      childrens = Lists.newArrayList();
      this.childrens = childrens;
    }
    childrens.add(res);
  }

  public long[] getParentIdsArray() {
    return StringUtil.split(this.getParentIds(), "/", 0L);
  }
}
